package com.rie06.openvoip;

import com.rie06.openvoip.client.Client;
import com.rie06.openvoip.client.Contact;
import com.rie06.openvoip.client.IHM.communication.CommunicationController;
import com.rie06.openvoip.client.IHM.home.InterfaceClient;
import com.rie06.openvoip.server.Server;
import com.rie06.openvoip.server.ServerUDP;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class Main implements Runnable {

    private static final Logger logger = Logger.getLogger(Main.class);
    private Server server;
    private ServerUDP serverUDP;
    private InterfaceClient ihm;
    private CommunicationController communicationController;
    private Client client;

    public Main() {
        /*
         * Initialize config Directory per user
         */

        boolean success = (new File(System.getProperty("user.home") + "/.config/OpenVoip")).mkdirs();
        if (!success) {
            boolean exists = (new File(System.getProperty("user.home") + "/.config/OpenVoip")).exists();
            if (!exists) {
                logger.log(Level.FATAL, "Unable to create config dir: ~/.config/OpenVoip");
                System.exit(0);
            }
        }

        logger.log(Level.DEBUG, "Starting server...");
        server = new Server();

        logger.log(Level.DEBUG, "Starting server on TCP port: 1995");
        serverUDP = new ServerUDP(server);
        logger.log(Level.DEBUG, "Starting server on UPD port: 1995");
        communicationController = new CommunicationController();
        ihm = new InterfaceClient(server, serverUDP, communicationController);
        client = new Client(server, serverUDP, ihm);

        /*
         * Comment Second part for production | \--->
         */

//        Thread thread = new Thread(this);
//        thread.setName("App");
//        thread.start();

    }

    public static void main(String[] args) {
        Main app = new Main();
    }

    public void run() {
        //Start second server
        logger.log(Level.DEBUG, "Starting server2...");
        Server server2 = new Server(1996);
        server2.getServerConfig().setUserName("userDebug");
        logger.log(Level.DEBUG, "Starting server2 on TCP port: 1996");
        ServerUDP serverUDP2 = new ServerUDP(server2);
        logger.log(Level.DEBUG, "Starting server2 on UPD port: 1996");
        CommunicationController communicationController2 = new CommunicationController();
        InterfaceClient ihm2 = new InterfaceClient(server2, serverUDP2, communicationController2);
        communicationController2.setInterfaceClient(ihm2);
        Client client2 = new Client(server2, serverUDP2, ihm2, true);

        try {
            //start new connection beetween client
            InetAddress ip = InetAddress.getByName("127.0.0.1");
            client.addContact(ip, 1996);

            //*********************************/
//            for (int i = 0; i < 3; i++) {
//                Contact c = new Contact(UUID.randomUUID(), "User"+i, InetAddress.getByName("127.0.0.1"), 1999);
//                client2.getContactList().put(c.getUuid(), c);
//            }

            Thread.sleep(10000);
            logger.log(Level.DEBUG, "Send test command...");

            client.sendMessage(server2.getServerConfig().getId(), "test");

            client2.sendMessage(server.getServerConfig().getId(), "mais non test2");



        } catch (InterruptedException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (UnknownHostException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }
}
